<?php

namespace App\V3\V3_2_7\Services;

use App\V3\V3_2_7\Models\AdminModel;
use App\V3\V3_2_7\Models\LoginLogModel;
use App\V3\V3_2_7\Models\LogOutStorageModel;
use App\V3\V3_2_7\Models\VipOrderModel;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;

class LoginLogService
{
    /**
     *返回活跃人数和支付信息
     * Author: zlf
     * Created at 2024/8/23 14:11
     * @param $data
     * @return JsonResponse
     */
    public static function getAccountInfo($data): JsonResponse
    {
        $outNum = LogOutStorageModel::query()
            ->where(function ($query) use ($data) {
                if(isset($data['start_time']))
                    $data['start_time'] = strtotime(Carbon::parse($data['start_time'])->startOfDay());
                if(isset($data['end_time']))
                    $data['end_time'] = strtotime(Carbon::parse($data['end_time'])->endOfDay());
                if (!empty($data['start_time']) && !empty($data['end_time'])) {
                    $query->whereBetween('created_at', [$data['start_time'], $data['end_time']]);
                } else if (!empty($data['start_time']) && empty($data['end_time'])) {
                    $query->where('created_at', '>=', $data['start_time']);
                } else if (empty($data['start_time']) && !empty($data['end_time'])) {
                    $query->where('created_at', '<=', $data['end_time']);
                } else {
                    $data['start_time'] = strtotime("today");
                    $data['end_time'] = strtotime("tomorrow") - 1;
                    $query->whereBetween('created_at', [$data['start_time'], $data['end_time']]);
                }
            })->distinct()
            ->orderBy('created_at', 'DESC')
            ->pluck('main_id')->toArray();
        $vipInfo = VipOrderModel::query()->select(
            DB::raw('count(*) AS order_num'),
            DB::raw('SUM(price) AS order_money'),
        )->where('status', 1)->first();
        $regPeoNum = AdminModel::query()->where([
            'delete_flag' => 0,
            'admin_role' => 2,
        ])->count();
        $vipInfo['reg_peo_num'] = $regPeoNum ?? 0;
        $vipInfo['lively_num'] = count($outNum) ?? 0;
        return success($vipInfo);
    }
}
